package com.ocean.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocean.admin.core.annotation.Log;
import com.ocean.admin.core.annotation.enums.BusinessType;
import com.ocean.admin.entity.SysRole;
import com.ocean.admin.model.dto.rule.SysRoleQueryDTO;
import com.ocean.admin.model.dto.rule.SysRoleSaveDTO;
import com.ocean.admin.model.dto.rule.SysRoleUpdateDTO;
import com.ocean.admin.model.vo.role.SysRoleDetailVo;
import com.ocean.admin.model.vo.role.SysRoleVo;
import com.ocean.admin.service.ISysRoleService;
import com.ocean.core.util.response.ResultSet;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/admin/sys-role")
public class SysRoleController {

    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 分页查询角色列表
     *
     * @param queryDTO 查询条件
     * @return 结果
     */
    @Operation(summary = "分页查询角色列表")
    @PostMapping(value = "pageList")
    @PreAuthorize("@ss.hasPermission('system:role:pageList')")
    public ResultSet<IPage<SysRoleVo>> pageList(@Validated @RequestBody SysRoleQueryDTO queryDTO) {
        return ResultSet.ok(iSysRoleService.pageList(queryDTO));
    }

    /**
     * 查询角色列表
     *
     * @return 结果
     */
    @Operation(summary = "查询角色列表")
    @GetMapping(value = "roleList")
    @PreAuthorize("@ss.hasPermission('system:role:pageList')")
    public ResultSet<List<SysRole>> roleList() {
        return ResultSet.ok(iSysRoleService.roleList());
    }

    /**
     * 查询角色详情
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermission('system:role:pageList')")
    @Operation(summary = "查询角色详情")
    @GetMapping(value = "roleInfo/{roleId}")
    public ResultSet<SysRoleDetailVo> roleInfo(@PathVariable Long roleId) {
        return ResultSet.ok(iSysRoleService.roleInfo(roleId));
    }

    /**
     * 新增角色
     *
     * @param saveDTO 角色信息
     * @return 结果
     */
    @Operation(summary = "新增角色")
    @PostMapping(value = "saveRole")
    @PreAuthorize("@ss.hasPermission('system:role:saveRole')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    public ResultSet<String> saveRole(@Validated @RequestBody SysRoleSaveDTO saveDTO) {
        return ResultSet.ok(iSysRoleService.saveRule(saveDTO));
    }

    /**
     * 删除角色
     *
     * @param roleIds 角色ID
     * @return 结果
     */
    @Operation(summary = "删除角色")
    @GetMapping(value = "removeRole/{roleIds}")
    @PreAuthorize("@ss.hasPermission('system:role:removeRole')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    public ResultSet<String> removeRole(@PathVariable Long[] roleIds) {
        return ResultSet.ok(iSysRoleService.removeRole(roleIds));
    }

    /**
     * 修改角色
     *
     * @param updateDTO 角色信息
     * @return 结果
     */
    @Operation(summary = "修改角色")
    @PostMapping(value = "updateRole")
    @PreAuthorize("@ss.hasPermission('system:role:updateRole')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    public ResultSet<String> updateRole(@Validated @RequestBody SysRoleUpdateDTO updateDTO) {
        return ResultSet.ok(iSysRoleService.updateRole(updateDTO));
    }

    /**
     * 修改角色状态
     *
     * @param roleId 角色ID
     * @param status 角色状态（0正常 1停用）
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermission('system:role:updateRole')")
    @Operation(summary = "修改角色状态")
    @GetMapping(value = "updateRoleStatus/{roleId}/{status}")
    public ResultSet<String> updateRoleStatus(@PathVariable Long roleId, @PathVariable Integer status) {
        return ResultSet.ok(iSysRoleService.updateRoleStatus(roleId, status));
    }
}
